<template>
	<!-- v-model="showModel" -->
	<view  mode="right" width="50%" height="100%">
		<view class="u-wrap">
			<view class="u-menu-wrap">
				<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
					<view v-for="(item,index) in list" :key="index" class="u-tab-item"
						:class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
						@tap.stop="swichMenu(index,item)">
						<text class="u-line-2" >{{item[labelName]}}</text>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'jobSelect',
		props: {
			//label展示字段
			labelName: {
				type: String,
				default: 'name'
			},
			//value选中字段
			valueName: {
				type: String,
				default: 'id'
			},
			//初始选中值
			selectValue: {
				type: Number,
				default: 0
			},
			//选择数据
			listData: {
				type: Array,
				default: () => {
					return [];
				}
			}
		},
		data() {
			return {
				list: JSON.parse(JSON.stringify(this.listData)),
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				value: this.selectValue,
				keyword: '',
				iconColor: 'primary',
				showAction: false,
				scrollHeight: 500
			}
		},
		created() {
			var that = this;
			uni.getSystemInfo({
				success: function(res) {
					let windowHeight = res.windowHeight;
					let windowWidth = res.windowWidth;
					//#ifdef H5
					let headHeight = 110 / 750 * windowWidth;
					//#endif
					//#ifndef H5
					let headHeight = 200 / 750 * windowWidth;
					//#endif
					let scrollHeight = (windowHeight - headHeight);
					that.scrollHeight = scrollHeight;

				}
			});
		},
		methods: {
			// get
			selval(item) {
				this.value = item[this.valueName];
				this.keyword = '';
				this.showAction = false;
				this.$emit('confirem', item[this.valueName]);
			},
			// 点击左边的栏目切换
			async swichMenu(index,item) {
				this.$emit('swichMenu',index ,item)
				if (index == this.current) return;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: 100%;
		width: 100%;
		display: flex;
		flex-direction: column;
		position: relative;
		overflow: hidden;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
		background-color: #ffffff;
	}

	.u-tab-view {
		width: 158rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 100rpx;
		background: #FFFFFF;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		font-size: 28rpx;
		padding: 0 20rpx;
		color: #444;
		font-weight: 400;
		text-align: center;
	}

	.u-tab-item-active {
		line-height: 1em;
		position: relative;
		color: #333333;
		font-size: 28rpx;
		font-weight: 600;
		background: #fff;
		color: #E93323;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
        width: 8rpx;
        background: linear-gradient(to bottom, #ff977b, #ff2d1b);
		height: 50rpx;
		left: 0;
		top: 28rpx;
	}
	.u-tab-view {
		height: 100%;
	}
	.right-box {
		background-color: white;
	}

	.page-view {
		background-color: white;
	}

	.class-item {
		background-color: #fff;
		border-radius: 8rpx;
	}


	.item-container {
		display: flex;
		flex-direction: column;
		flex: 1;
	}

	.item-active {
		color: $u-type-primary;
	}

	.thumb-box {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		height: 100rpx;
		width: 100%;
		padding: 0 20rpx;
		align-items: center;
		font-size: 32rpx;
	}
</style>
